// packageStore/pages/profile/addSubAccount/index.js
import {util,forms} from '../../../../js/util';
import { password}  from "../../../../js/modules/account.js";
import BasePage from '../../../../js/common/base-page'
var app = getApp();
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    customFileds:[
      {FieldName: "登录账号：",Id:'number',IsEnable:1,value:'',IsRequired:1,FieldType:"number",Placeholder: "请输入登录手机号"},
      {FieldName: "登录密码：",Id:'password',IsEnable:1,value:'',IsRequired:1,FieldType:"password",Placeholder: "请输入登录密码"},
      {FieldName: "员工姓名：",Id:'name',IsEnable:1,value:'',IsRequired:1,FieldType:"text",Placeholder: "请输入员工姓名"},
      {FieldName: "昵称：",Id:'nickname',IsEnable:1,value:'',IsRequired:0,FieldType:"text",Placeholder: "请输入昵称"}
    ],//
    customInfosList: [], 
    status:true,
    orderStatus:true,
    type:'',
    customItem:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.editList(options.type,options.type == 'edit'?JSON.parse(options.item):{})
    this.setData({
      type:options.type
    })
    wx.setNavigationBarTitle({
      title: options.type == 'edit'?'修改子账号':'添加子账号' 
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  editList(type,item){
    var that = this;
    if(type == 'edit'){
      var customInfosList = that.data.customInfosList;
      var customFileds = that.data.customFileds;//原始数据
      customInfosList.push({dataId:'number',value:item.UserName},{dataId:'password',value:item.Password},{dataId:'name',value:item.Name},{dataId:'nickname',value:item.NickName})
      customFileds.forEach(function(list){
        if(list.Id == 'number'){
          list.value = item.UserName
        }
        // else if(list.Id == 'password'){
        //   list.value = item.Password
        // }
        else if(list.Id == 'name'){
          list.value = item.Name
        }else if(list.Id == 'nickname'){
          list.value = item.NickName
        }
      })
      // console.log(customFileds,'=====customFileds');
      that.setData({
        customItem:item,
        customInfosList:customInfosList,
        customFileds:customFileds,
        status:item.Status,
        orderStatus:item.IsValidOrder
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  inputCallBack(e){
    this.dealCustomInfosList(e)
  },
   // 公用方法
   dealCustomInfosList(event) {
    let value = event.detail
    if(!value) {
      wx.showToast({
        title: '数据格式错误',
        icon: 'none'
      })
      return
    }
    let customInfosList = this.data.customInfosList
    let flag = false
    if(customInfosList.length <= 0) {
      customInfosList.push(value)
      return
    }
    for(var i = 0; i < customInfosList.length; i++) {
      if(customInfosList[i].dataId === value.dataId) {
        customInfosList[i] = value
        flag = true
        break
      }
    }
    if(!flag) {
      customInfosList.push(value)
    }
    this.setData({
      customInfosList: customInfosList
    })
    
  },
  checkCustomInfo() {
    let flag = true
    let customInfosList = this.data.customInfosList // 已填写的信息
    let customFileds = this.data.customFileds // 原始数据
    let toastText = '';
    console.log(customInfosList,'=1111');
    // 未填写任何信息
    if(customInfosList.length <= 0) {
      for(let i = 0; i < customFileds.length; i++) {
        if(customFileds[i].IsRequired === 1) {
          flag = false
          toastText = `${customFileds[i].FieldName}未填写`
          break
        }
      }
    } else{
      for(let i = 0; i < customFileds.length; i++) {
        if(customFileds[i].IsRequired === 1) {
          let mFlag = true
          for(let j = 0; j < customInfosList.length; j++) {
            if(customFileds[i].Id === customInfosList[j].dataId) {
              mFlag = false
              if(customInfosList[j].value.length <= 0) {
                flag = false
                toastText = `请完善${customFileds[i].FieldName}`
                break
              }
            }
          }
          if(mFlag) {
            flag = false
            toastText = `请完善${customFileds[i].FieldName}`
            break
          }
        }
      }
    }
    if(!flag) {
      util.tips(toastText);
      return false
    }
    return true
  },
  /*保存*/
  bindSave:function(){
    var that = this;
    // 检查自定义订单信息
    const customFlag = that.checkCustomInfo()
    if(!customFlag) {
      return
    }
    let customInfosList = that.data.customInfosList;
    var number='',passwords='',name = '',nickname='';
    for(var i=0;i<customInfosList.length;i++){
      if(customInfosList[i].dataId == 'number'){
        number = customInfosList[i].value;
      }else if(customInfosList[i].dataId == 'password'){
        passwords = customInfosList[i].value;
      }else if(customInfosList[i].dataId == 'name'){
        name = customInfosList[i].value;
      }else if(customInfosList[i].dataId == 'nickname'){
        nickname = customInfosList[i].value;
      }
    }
    var reg = forms.validateRegExp["mobile"];
    // var regs = forms.validateRegExp["pwd"];
    if(!reg.test(number)){
      util.tips('请输入正确的手机号！！！');
      return;
    }else if(passwords.length < 6 ){
      util.tips('请输入正确的登录密码！！！');
      return;
    }
    var data = {UserName:number,Password:passwords,Name:name,NickName:nickname,Status:that.data.status,IsValidOrder:that.data.orderStatus}
    if(that.data.type == 'edit'){
      data.Id = that.data.customItem.Id;
      password.managerUpdate(data,function(res){
        wx.navigateBack({//返回
          delta: 1
        })
      })
    }else{
      password.managerAdd(data,function(res){
        wx.navigateBack({//返回
          delta: 1
        })
      })
    }
    
  },
  switch1Change(e){
    this.setData({
      status:e.detail.value
    })
  },
  bindSubmit(e){
    this.setData({
      orderStatus:e.detail.value
    })
  },
})